package com.travel.web.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.travel.model.*;
import com.travel.service.IContactInfoService;
import com.travel.service.IOrderInfoService;
import com.travel.service.IRouteService;
import com.travel.service.IWebUserService;
import com.travel.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class IndexController {


    @Autowired
    IWebUserService webUserService;

    @Autowired
    IRouteService routeService;

    @Autowired
    IOrderInfoService orderInfoService;


    @Autowired
    IContactInfoService contactInfoService;

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/packages")
    public String packages(Model model) {
        Route route = new Route();
        List<Route> routes = routeService.selectRouteList(route);
        model.addAttribute("list", routes);
        Object principal = SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user", principal);
        return "packages";
    }

    @PostMapping("/bookNow")
    @ResponseBody
    public AjaxResult bookNow(String routeId) {

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setRouteId(Long.valueOf(routeId));
        WebUser webUser = (WebUser) SecurityUtils.getSubject().getPrincipal();
        orderInfo.setUserId(webUser.getUserId());
        orderInfo.setCreateTime(new Date());
        orderInfoService.insertOrderInfo(orderInfo);
        return AjaxResult.success();
    }

    @PostMapping("/contactInfo")
    @ResponseBody
    public AjaxResult contactInfo(ContactInfo contactInfo) {
        contactInfo.setCreateTime(new Date());
        contactInfoService.insertContactInfo(contactInfo);
        return AjaxResult.success();
    }


    @GetMapping("/services")
    public String services() {
        return "services";
    }

    @GetMapping("/gallery")
    public String gallery() {
        return "gallery";
    }

    @GetMapping("/guide")
    public String guide(Model model) {
        Route route = new Route();
        List<Route> routes = routeService.selectRouteList(route);
        model.addAttribute("list", routes);
        Object principal = SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user", principal);
        return "guide";
    }

    @GetMapping("/detail")
    public String detail(String id, Model model) {
        Route route = routeService.selectRouteById(Long.valueOf(id));
        model.addAttribute("route", route);
        return "detail";
    }

    @GetMapping("/contact")
    public String contact() {
        return "contact";
    }

    @GetMapping("/evaluate")
    public String evaluate() {
        return "evaluate";
    }


    @GetMapping("/")
    public String home() {
        return "index";
    }


    @PostMapping("/routeList")
    public void list(Route route, Model model) {

    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(String email, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(email, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return AjaxResult.success();
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return AjaxResult.error(msg);
        }

    }

    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(String email, String password) {
        WebUser webUser = new WebUser();
        webUser.setEmail(email);
        webUser.setPassword(password);
        webUserService.insertUser(webUser);
        return AjaxResult.success();
    }


}
